#!/usr/bin/env python
import os, sys
import numpy as np
from astropy.io import fits

if len(sys.argv) < 3:
	print('usage: {} img1.fits img2.fits'.format(sys.argv[0]))
	print('  output img1.fits - img2.fits')
	sys.exit(0)

print('> subtracting {} from {}'.format(sys.argv[2], sys.argv[1]))

d1 = fits.getdata(sys.argv[1]).astype(float)
d2 = fits.getdata(sys.argv[2]).astype(float)


fout = 'diff.fits'

if len(sys.argv) == 4:
	fout = sys.argv[3]

fits.writeto(fout, data=(d1-d2).astype(np.int16), overwrite=True)

os.system('ds9 diff.fits &')

